package com.example.neartweet;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class PollActivity extends Activity {

	private EditText query;
	private Button pollButton;
	private LinearLayout layout;
	private String message;
	private EditText option1;
	private EditText option2;
	private EditText option3;
	private EditText option4;
	private boolean location;
	private String opt1 = null;
	private String opt2 = null;
	private String opt3 = null;
	private String opt4 = null;
	private int res1 = 0;
	private int res2 = 0;
	private int res3 = 0;
	private int res4 = 0;
	
	public GraphicalView createChart() {
        String[] titles = new String[] { "Poll"};
        List<double[]> values = new ArrayList<double[]>();
          
        //values.add(new double[] { 5230, 7300, 9240,10230});
        values.add(new double[] { res1, res2, res3, res4});
 
        int[] colors = new int[] { Color.parseColor("#77c4d3")};
        XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
        renderer.setOrientation(Orientation.HORIZONTAL);
        setChartSettings(renderer, "Results", "Options", "Votes", 0.5,
            12.5, 0, 25, Color.BLACK, Color.BLACK);
        renderer.setXLabels(1);
        renderer.setYLabels(10);
        renderer.addXTextLabel(1, opt1);
        renderer.addXTextLabel(2, opt2);
        renderer.addXTextLabel(3, opt3);
        renderer.addXTextLabel(4, opt4);
        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
          SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
          seriesRenderer.setDisplayChartValues(true);
        }
        
        final GraphicalView grfv = ChartFactory.getBarChartView(this, buildBarDataset(titles, values), renderer,BarChart.Type.DEFAULT);
        return grfv;
	}
	
	protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(16);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setBarSpacing(1);
         
        renderer.setMarginsColor(Color.parseColor("#EEEDED"));
        renderer.setXLabelsColor(Color.BLACK);
        renderer.setYLabelsColor(0,Color.BLACK);
         
        renderer.setApplyBackgroundColor(true);
        renderer.setBackgroundColor(Color.parseColor("#FBFBFC"));
         
        int length = colors.length;
        for (int i = 0; i < length; i++) {
          SimpleSeriesRenderer r = new SimpleSeriesRenderer();
          r.setColor(colors[i]);
          r.setChartValuesSpacing(15);
          renderer.addSeriesRenderer(r);
        }
        return renderer;
    }
	
  protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        int length = titles.length;
        for (int i = 0; i < length; i++) {
          CategorySeries series = new CategorySeries(titles[i]);
          double[] v = values.get(i);
          int seriesLength = v.length;
          for (int k = 0; k < seriesLength; k++) {
            series.add(v[k]);
          }
          dataset.addSeries(series.toXYSeries());
        }
        return dataset;
      }
  protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
          String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
          int labelsColor) {
        renderer.setChartTitle(title);
        renderer.setYLabelsAlign(Align.RIGHT);
        renderer.setXTitle(xTitle);
        renderer.setYTitle(yTitle);
        renderer.setXAxisMin(xMin);
        renderer.setXAxisMax(xMax);
        renderer.setYAxisMin(yMin);
        renderer.setYAxisMax(yMax);
        renderer.setMargins(new int[] { 10, 65, 10, 15 });
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);
      }

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_poll);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setTitle("Poll");
		query = (EditText) findViewById(R.id.InsertTopic);
        pollButton = (Button) findViewById(R.id.Poll);
        option1 = (EditText) findViewById(R.id.option1);
        option2 = (EditText) findViewById(R.id.option2);
        option3 = (EditText) findViewById(R.id.option3);
        option4 = (EditText) findViewById(R.id.option4);
        layout = (LinearLayout)findViewById(R.id.chart);
        final GraphicalView gv = createChart();
        layout = (LinearLayout)findViewById(R.id.chart);
        layout.addView(gv);
        
        option1.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {

				opt1 = option1.getText().toString();

			}
		});
        
        option2.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {

				opt2 = option2.getText().toString();

			}
		});
        
        option3.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {

				opt3 = option3.getText().toString();

			}
		});
        
        option4.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {

				opt4 = option4.getText().toString();

			}
		});
        
        SharedPreferences sharedPrefs = getSharedPreferences(
		        getString(R.string.preference_file_key),MODE_PRIVATE);
		int state = sharedPrefs.getInt("gps", 0);
		if (state == 0){
			location = false;
		} else {
			location = true;
		}

        pollButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	SharedPreferences sharedPrefs = getSharedPreferences(
        		        getString(R.string.preference_file_key),MODE_PRIVATE);
        		String uN = sharedPrefs.getString("usr", "");
        		message = "Poll:" + uN + ":";
        		if(option1.getText().toString().equals("") == false){
        			message += option1.getText().toString();
        		}
                if(option2.getText().toString().equals("") == false){
        			message += option2.getText().toString();
        		}
                if(option3.getText().toString().equals("") == false){
        			message += option3.getText().toString();
        		}
                if(option4.getText().toString().equals("") == false){
        			message += option4.getText().toString();
        		}
                query.setText("");
            }
        });
	}

	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.poll, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		SharedPreferences sharedPrefs = getSharedPreferences(
		        getString(R.string.preference_file_key),MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPrefs.edit();
		switch (item.getItemId()) {
		case R.id.action_settings:
			intent = new Intent(this, SettingsActivity.class);
			startActivity(intent);
			break;
		case R.id.gps:
			if(location) {
				item.setIcon(R.drawable.gps_off);
				location = false;
				
				editor.putInt("gps", 0);
				editor.commit();
			} else {
				item.setIcon(R.drawable.gps_on);
				location = true;
				
				editor.putInt("gps", 1);
				editor.commit();
			}
			break;
		case R.id.compose:
			intent = new Intent(this, ComposeActivity.class);
			startActivity(intent);
			PollActivity.this.finish();
			break;
		case android.R.id.home:
			PollActivity.this.finish();
			break;
		default:
			
		}
		return super.onOptionsItemSelected(item);
	}

}
